﻿using Business;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApi.Controllers
{
    public class PickLocationReplenishController : ApiController
    {
        private static BLocationReplenish _NewInstance = new BLocationReplenish();

        public static BLocationReplenish NewInstance
        {
            get
            {
                if (_NewInstance == null)
                {
                    _NewInstance = new BLocationReplenish();
                }

                return _NewInstance;
            }
        }

        /// <summary>
        /// 获取拣货商品货位信息成功
        /// </summary>
        /// <param name="GoodsCode"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel GetPickLocationInfo(string GoodsCode)
        {
            if (string.IsNullOrEmpty(GoodsCode))
            {
                return new ResultModel { RESULT = "failed", MESSAGE = "请输入参数值：拣货商品编号/货位号", DATA = null };
            }

            var list = NewInstance.GetPickLocationGoodsInfo(GoodsCode);
            if (list != null && list.Count > 0)
            {

                return new ResultModel { RESULT = "success", MESSAGE = "获取成功", DATA = list };
            }
            else
            {
                if (list == null)
                {
                    return new ResultModel { RESULT = "failed", MESSAGE = "获取拣货商品货位信息失败", DATA = null };
                }
                else
                {
                    return new ResultModel { RESULT = "failed", MESSAGE = "该商品/货位号无拣货货位信息，请查询货位管理系统", DATA = null };
                }
            }
        }

        [HttpGet]
        public ResultModel GetPickCellNo()
        {
            return new ResultModel { RESULT = "success", MESSAGE = "获取成功", DATA = NewInstance.GetPickCellNo() };
        }
        [HttpGet]
        public ResultModel GetPickLocationGoods(string batchNo, DateTime rationDate)
        {
            if (string.IsNullOrEmpty(batchNo) || string.IsNullOrEmpty(rationDate.ToString()))
            {
                return new ResultModel { RESULT = "failed", MESSAGE = "参数有空值", DATA = NewInstance.GetPickCellNo() }; 
            }
            else
            {
                var _resultLst = NewInstance.GetPickLocationGoods(batchNo, rationDate);
                if (_resultLst == null || _resultLst.Count <= 0)
                    return new ResultModel { RESULT = "failed", MESSAGE = "获取信息失败", DATA = null }; 
                else
                    return new ResultModel { RESULT = "success", MESSAGE = "获取成功", DATA = _resultLst };
            }
        }
        [HttpPost]
        public ResultModel UpdateReplenishStatus(string userID, List<PickReplenishInfo> lstPickReplenishInfo)
        {
            if (lstPickReplenishInfo == null || lstPickReplenishInfo.Count <= 0)
                return new ResultModel { RESULT = "failed", MESSAGE = "参数有空值", DATA = null }; 
            else if (NewInstance.UpdateReplenishStatus(userID, lstPickReplenishInfo))
                return new ResultModel { RESULT = "success", MESSAGE = "提交成功", DATA = null };
            else
                return new ResultModel { RESULT = "failed", MESSAGE = "分拣货位补货状态更新失败", DATA = null }; 

        }

        [HttpGet]
        public ResultModel PickTaskQuery(string locationCode, string batchNo, string rationDate)
        {

            if (string.IsNullOrWhiteSpace(locationCode) || string.IsNullOrWhiteSpace(batchNo) || string.IsNullOrWhiteSpace(rationDate))
                return new ResultModel { RESULT = "failed", MESSAGE = "参数有空值", DATA = null }; 

            List<PickReplenishInfo> lstOrderItemInfo = NewInstance.PickTaskQuery(locationCode, batchNo, rationDate);
            if (lstOrderItemInfo.Count() <= 0)
                   return new ResultModel { RESULT = "failed", MESSAGE = "没有数据", DATA = null };
            return new ResultModel { RESULT = "success", MESSAGE = "提交成功", DATA = lstOrderItemInfo };
        }
    }
}